package com.sxz.sbs.module.staff;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.entity.AuthorityEntity;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.client.AllHousesActivity;
import com.sxz.sbs.module.client.presenter.ClientManagementPresenter;
import com.sxz.sbs.module.client.view.ClientManagementView;
import com.sxz.sbs.module.staff.adapter.StaffMangerAdapter;
import com.sxz.sbs.router.RouterUrl;
import com.sxz.sbs.widget.CommonTitle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 员工管理
 *
 * @author hp
 */
@Route(path = RouterUrl.STAFF_MANGER)
public class StaffMangerActivity extends BaseActivity<ClientManagementPresenter> implements SwipeRefreshLayout.OnRefreshListener
        , ClientManagementView {

    @Autowired
    public int id;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private StaffMangerAdapter staffMangerAdapter;
    private List<AuthorityEntity> data = new ArrayList<>();

    /**
     * 初始化
     */
    @Override
    protected void init() {
        ARouter.getInstance().inject(this);
        new CommonTitle.Builder(this).setTitle("员工管理").build().show();
        recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setRefreshing(true);
        getData();
        staffMangerAdapter = new StaffMangerAdapter(data);
        recyclerView.setAdapter(staffMangerAdapter);
        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (RouterUrl.DISTRIBUTE_CUSTOM.equals(data.get(position).getUrl_ClickUrl())) {
                    startActivity(AllHousesActivity.class);
                } else if (RouterUrl.MANAGER_PROMOT.equals(data.get(position).getUrl_ClickUrl())) {
                    ToastUtils.showShort("该功能开发中，敬请期待！");
                } else {
                    ARouter.getInstance()
                            .build(data.get(position).getUrl_ClickUrl())
                            .withString("status", data.get(position).getTxt_Para())
                            .navigation();
                }
            }
        });
        stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
            @Override
            public void onRetry(int state) {
                getData();
            }
        });
    }

    /**
     * 获取数据
     */
    public void getData() {
        HashMap<String, Object> map = HttpParamMap.getParamMap();
        map.put("fatherId", id);
        mPresenter.getClientManagementList(map);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_staff_manger;
    }

    @Override
    protected ClientManagementPresenter createPresenter() {
        return new ClientManagementPresenter();
    }


    @Override
    public void onRefresh() {
        getData();
    }

    @Override
    public void onClientManagementSuccess(List<AuthorityEntity> list) {
        if (mIsLive) {
            refreshLayout.setRefreshing(false);
            if (data != null) {
                data.clear();
                data.addAll(list);
                staffMangerAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void loginAgain() {
        super.loginAgain();
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void showError(int errorTag, String msg) {
        refreshLayout.setRefreshing(false);
        if (errorTag == AppConstant.NET_ERROR_CODE) {
            stateLayout.showNetwork();
        } else {
            stateLayout.showError();
        }
    }
}
